/*
 * Spring 2012 TCSS360 Deliverable 3
 * Richard Nguyen
 */

package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import model.Auction;

@SuppressWarnings("serial")
public class ItemFormGUI extends JPanel {

	private final JFrame my_frame = new JFrame();

	private static final String ITEM_NAME = "Item Name";

	private static final String NPO = "NPO";

	private static final String MINIMUN_BID = "Minimun Bid";

	private static final String CONDITION = "Condition";

	private static final String QUANTITY = "Quantity";

	private static final String COMMENTS = "   Comments";

	private static final int COMMENT_COL = 350;

	private static final int COMMENT_ROW = 80;

	private static final String SUBMIT = "Submit";

	private static final String CLEAR = "Clear";

	private JTextField my_Item_Name;

	private JTextField my_Npo;

	private JTextField my_Minimun_Bid;

	private JTextField my_Condition;

	private JTextField my_Quantity;

	private JTextArea my_Comments;

	private final JButton my_submit;

	private final JButton my_clear;

	private JPanel north;

	private JPanel south_north;

	private Auction my_auction;

	private AuctionGUI my_auction_gui;

	public ItemFormGUI(final Auction the_auction,
			final AuctionGUI the_auction_gui) {
		super();

		my_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		my_frame.setResizable(false);
		setLayout(new BorderLayout());
		my_auction = the_auction;
		my_auction_gui = the_auction_gui;

		my_submit = new JButton(SUBMIT);
		my_clear = new JButton(CLEAR);
		my_submit.addActionListener(new SubmitListener());
		my_clear.addActionListener(new ClearListener());

		onFrameNorth();
		onFrameSouth();

		SpringUtilities.makeCompactGrid(north, 5, 2, 6, 6,
				6, 6);

		SpringUtilities.makeCompactGrid(south_north, 1, 2,
				6, 6, 6, 6);

		start();
	}

	public void start() {
		my_frame.pack();
		my_frame.setVisible(true);
		my_frame.setLocationRelativeTo(null);
	}

	private void onFrameNorth() {
		north = new JPanel(new SpringLayout());

		my_Item_Name = new JTextField();
		my_Npo = new JTextField();
		my_Npo.setText(my_auction.getNPO());
		my_Npo.setEditable(false);
		my_Minimun_Bid = new JTextField();
		my_Condition = new JTextField();
		my_Quantity = new JTextField();

		final JLabel Item_Name_Label =
				new JLabel(ITEM_NAME, JLabel.TRAILING);
		north.add(Item_Name_Label);
		north.add(my_Item_Name);

		final JLabel NPO_Label =
				new JLabel(NPO, JLabel.TRAILING);
		north.add(NPO_Label);
		north.add(my_Npo);

		final JLabel Minimun_Bid_Label =
				new JLabel(MINIMUN_BID, JLabel.TRAILING);
		north.add(Minimun_Bid_Label);
		north.add(my_Minimun_Bid);

		final JLabel Condition_Label =
				new JLabel(CONDITION, JLabel.TRAILING);
		north.add(Condition_Label);
		north.add(my_Condition);

		final JLabel Quantity_Label =
				new JLabel(QUANTITY, JLabel.TRAILING);
		north.add(Quantity_Label);
		north.add(my_Quantity);

		my_frame.add(north, BorderLayout.NORTH);
	}

	private void onFrameSouth() {
		JPanel south = new JPanel(new BorderLayout());
		south_north = new JPanel(new SpringLayout());
		my_Comments = new JTextArea();
		final JScrollPane areaScrollPane =
				new JScrollPane(my_Comments);

		final JLabel Comments_Label =
				new JLabel(COMMENTS, JLabel.TRAILING);
		south_north.add(Comments_Label);
		south_north.add(areaScrollPane);

		areaScrollPane.setPreferredSize(new Dimension(
				COMMENT_COL, COMMENT_ROW));

		final JPanel south_south = new JPanel();
		south_south.add(my_submit);
		south_south.add(my_clear);

		south.add(south_north, BorderLayout.NORTH);
		south.add(south_south, BorderLayout.SOUTH);

		my_frame.add(south, BorderLayout.SOUTH);
	}

	private class SubmitListener implements ActionListener {
		public void actionPerformed(
				final ActionEvent the_action) {

			try {
				final BigDecimal minBid_Big =
						new BigDecimal(
								my_Minimun_Bid.getText());
				final String item_string =
						my_Item_Name.getText();
				final String condition_string =
						my_Condition.getText();
				final int quantity_int =
						Integer.parseInt(my_Quantity
								.getText());
				final String comment_string =
						my_Comments.getText();

				ItemFormGUI.this.my_auction.addItem(
						item_string, quantity_int,
						minBid_Big, condition_string,
						comment_string);

				JOptionPane.showMessageDialog(
						ItemFormGUI.this,
						"Item successfully added!",
						"Item added",
						JOptionPane.INFORMATION_MESSAGE);
				ItemFormGUI.this.my_frame.dispose();

				if (ItemFormGUI.this.my_auction
						.getItemAdded()) {
					my_auction_gui
							.initialize_items_list(ItemFormGUI.this.my_auction
									.getItems());
					ItemFormGUI.this.my_auction
							.setItemAdded();
				}
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(
						ItemFormGUI.this,
						"Mininum bid must be real number",
						"Invalid mmininum bid",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	private class ClearListener implements ActionListener {
		public void actionPerformed(
				final ActionEvent the_action) {
			my_Item_Name.setText("");
			my_Minimun_Bid.setText("");
			my_Condition.setText("");
			my_Quantity.setText("");
			my_Comments.setText("");
		}
	}
}
